package com.example.thread.d3取钱同步方法;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
    private String cardId;
    private double money;

    // 小明，小红同时来取钱
    public synchronized void drawMoney(double money) {
        // 先搞清楚谁来取钱
        String name = Thread.currentThread().getName();
        // 取钱
        if (this.money >= money) {
            System.out.println(name + "取钱：" + money + "成功");
            this.money -= money;
            System.out.println(name + "来取钱后，余额剩余：" + this.money);
        } else {
            System.out.println(name + "来取钱，余额不足！");
        }
    }
}
